package com.loongxun.playwithyou.ui.activity.playuser;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;

import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.Order;
import com.loongxun.playwithyou.bean.SendRequest;
import com.loongxun.playwithyou.network.MyHttpClientRespenseHandler;
import com.loongxun.playwithyou.network.nHttpClient;
import com.loongxun.playwithyou.ui.adapter.OrderAdapter;
import com.loongxun.playwithyou.util.ToastView;
import com.loongxun.playwithyou.view.pullableview.PullToRefreshLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 陪玩人员结算明细列表
 * Created by Administrator on 2017/8/8.
 */
public class BillInfoListActivity extends Activity {

    private Context context;
    private SendRequest send;

    //标题
    private TextView tv_top_title;

    private List<Order> orders = new ArrayList<>();
    private ListView listView;
    private PullToRefreshLayout ptrl;
    private TextView loadstate_tv;
    private OrderAdapter mAdapter;

    private String statisId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_bill_list);
        context = BillInfoListActivity.this;

        statisId = getIntent().getStringExtra("statisId");

        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("明细列表");
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        ptrl = ((PullToRefreshLayout) findViewById(R.id.refresh_view));
        loadstate_tv = (TextView) findViewById(R.id.loadstate_tv);
        ptrl.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                // 下拉刷新操作
                new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        // 千万别忘了告诉控件刷新完毕了哦！
                        pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                }.sendEmptyMessageDelayed(0, 3000);
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                // 下拉刷新操作
                new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        loadstate_tv.setText("已加载全部数据");
                        // 千万别忘了告诉控件刷新完毕了哦！
                        pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                }.sendEmptyMessageDelayed(0, 3000);

            }
        });
        listView = (ListView) findViewById(R.id.content_view);

        initData();
        ptrl.autoRefresh();
    }

    private void initData() {
        try {
            send = new SendRequest();
            send.statisId = statisId;
            //判断此会员是否为陪玩人员
            nHttpClient.getAll(ApiInterface.BILL_ORDER_LIST, send.toBillInfoListJson().toString(), orderMemberHandler);
        } catch (Exception e) {
            ToastView toast = new ToastView(context, "参数错误");
            toast.show();
        }
    }

    private MyHttpClientRespenseHandler orderMemberHandler = new MyHttpClientRespenseHandler() {

        @Override
        public void success(JSONObject json) {
            Integer succeed = json.optJSONObject("status").optInt("succeed", 0);
            JSONArray itemArray;
            if (succeed == 1) {
                try {
                    itemArray = json.getJSONObject("data").getJSONArray("playOrders");
                    for (int i = 0; i < itemArray.length(); i++) {
                        JSONObject subItemObject = itemArray.getJSONObject(i);
                        Order order = new Order();
                        order.setHours_num(subItemObject.getInt("hours_num"));
                        order.setOrder_no(subItemObject.getString("order_no"));
                        order.setPlay_start_time(subItemObject.getString("play_start_time"));
                        orders.add(order);
                    }
                    mAdapter = new OrderAdapter(context, orders, 1);
                    mAdapter.setStatisId(statisId);
                    listView.setAdapter(mAdapter);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                String error_desc = json.optJSONObject("status").optString("error_desc");
                new ToastView(context, error_desc).show();
            }
        }

        @Override
        public void faile(String json) {
            new ToastView(context, json).show();
        }
    };
}
